#include "chooselevelscene.h"
#include<QMenuBar>
#include<QPainter>
#include "mypushbutton.h"
#include<QDebug>
#include<QTimer>
#include<QLabel>
#include<QApplication>
#include<QSound>
//#include<Q_Object>
#define Q_OBJECCT
ChooseLevelScene::ChooseLevelScene(QWidget *parent) : QMainWindow(parent)
{


    this->setFixedSize(320,588);

    this->setWindowIcon(QPixmap(":/res/Coin0001.png"));

    this->setWindowTitle("选关卡");
    //创建菜单栏
    QMenuBar *bar=menuBar();
    setMenuBar(bar);
    //创建开始菜单
    QMenu *startMenu=bar->addMenu("开始");
    //创建退出菜单项
    QAction *quitAction=startMenu->addAction("退出");
    //点击退出，实现退出游戏
    connect(quitAction,&QAction::triggered,[=](){
        this->close();
    });

    //选择关卡按钮音效
    QSound *chooseSound =new QSound(":/res/TapButtonSound.wav",this);
    //返回按钮音效
    QSound *backSound=new QSound(":/res/BackButtonSound.wav",this);
    //返回按钮
    MyPushButton *backBtn=new MyPushButton(":/res/BackButton.png",":/res/BackButtonSelected.png");
    backBtn->setParent(this);
    backBtn->move(this->width()-backBtn->width(),this->height()-backBtn->height());
    connect(backBtn,&MyPushButton::clicked,[=](){
        qDebug()<<"点击了返回按钮";
        //告诉主场景，我返回了，主场景监听ChooseLevelScene的返回按钮
        //延时返回
        //播放返回按钮声效
        backSound->play();
        QTimer::singleShot(100,this,[=](){
             emit this->chooseSceneBack();
        });

    });
    //创建选择关卡的按钮
    for(int i=0;i<20;i++)
    {
        MyPushButton *menubtn=new MyPushButton(":/res/LevelIcon.png");
        menubtn->setParent(this);
        menubtn->move(25+i%4*70,130+i/4*70);
        //监听每个按钮的点击事件
        connect(menubtn,&MyPushButton::clicked,[=](){
            //播放选择关卡按钮
            chooseSound->play();
            QString str=QString("您选择的是第 %1 关").arg(i+1);
            qDebug()<<str;
            //进入游戏场景
            this->hide();//将选关场景隐藏
            play=new PlayScene(i+1);//创建游戏场景
            play->show();//显示游戏场景
             //设置游戏场景的初始位置
            play->setGeometry(this->geometry());
           //游戏场景中的back按钮
            connect(play,&PlayScene::chooseSceneBack,[=](){
                    qDebug()<<"已返回";

                 //设置固定窗口
                  this->setGeometry(play->geometry());

                 this->show();

               delete play;
                play=NULL;
            });

        });
        QLabel *label=new QLabel;
        label->setParent(this);
        label->setFixedSize(menubtn->width(),menubtn->height());
        label->setText(QString::number(i+1));
        label->move(25+ i%4*70,130+ i/4 *70);

        //设置lablel 上的文字对齐方式，水平居中和垂直居中
        label->setAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
        //设置让鼠标进行穿透
        label->setAttribute(Qt::WA_TransparentForMouseEvents);
    }
}

void ChooseLevelScene::paintEvent(QPaintEvent *)
{
    QPainter painter(this);
    QPixmap pix;
    pix.load(":/res/OtherSceneBg.png");
painter.drawPixmap(0,0,this->width(),this->height(),pix);
     pix.load(":/res/Title.png");
     painter.drawPixmap((this->width()-pix.width())*0.5,30,pix.width(),pix.height(),pix);
};

